package com.my.test.java.two.datasource;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.Set;

public class MyDataSource {

	public static String driveClass;
	private String url;
	private String userName;
	private String pwd;
	private static LinkedList<Connection> list = new LinkedList<Connection>();
	static{
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public MyDataSource(String url,String userName,String pwd) throws SQLException{
		for(int i=0;i<3;i++){
			Connection con = DriverManager.getConnection(url, userName, pwd);
			list.add(con);
		}
	}
	public void freeConnection(Connection conn) { 
        list.add(conn);
	}
	public Connection getConn() throws SQLException {
		if(list.size()>0){
			return list.removeFirst();
		}
		throw new ExceptionInInitializerError();
	}
	
	
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
}
